<?php
/**
 * Created by PhpStorm.
 * User: Steven
 * Date: 2017/6/6
 */

namespace app\educational\model;


use think\Db;
use think\Model;

class TeacherLeaveModel extends Model
{
    public function __construct($data = [])
    {
        parent::__construct($data);
    }

    /**
     * 获取请假列表请假
     * @param array $filter
     * @param array $order
     * @param int $limit
     * @param $query
     * @return \think\Paginator
     */
    public function getLeave($filter=[], $order=[], $limit=10, $query)
    {
        //默认条件
        $where = [
            'tl.delete_time' => ['=', 0],
        ];
        if(!empty($filter) && is_array($filter)){
            $where = array_merge($where,$filter);
        }

        //默认排序
        $sort = ['tl.id'=>'desc'];
        if(!empty($order) && is_array($order)){
            $sort = array_merge($order,$sort);
        }

        //分页url额外参数
        $page_query['query'] = [];
        if(!empty($query) && is_array($query)){
            $page_query['query'] = $query;
        }

        $data = $this->alias('tl')
            ->field('tl.*,sc.name as campus_name,cl.name as class_name,u.realname as teacher_name,
            us.realname as supply_name')
            ->join('class cl','tl.class_id=cl.id')
            ->join('school sc','cl.school_id=sc.id')
            ->join('user u','tl.teacher_id=u.id')
            ->join('user us','tl.supply_teacher=us.id','left')
            ->where($where)
            ->order($sort)
            ->paginate($limit,false,$page_query);

        return $data;
    }

    /**
     * 编辑请假
     * @param array $data
     * @return bool
     */
    public function editLeave($data=[])
    {
        $result = true;
        $id = intval($data['id']);

        $oldLeave = $this->where('id', $id)->find();
        if(!isset($oldLeave->id)){
            $result = false;
        }else{
            self::startTrans();
            try{
                $this->isUpdate(true)->allowField(true)->save($data, ['id' => $id]);

                self::commit();
            }catch(\Exception $e){
                self::rollback();
                $result = false;
            }
        }

        return $result;
    }

    /**
     * 通过字段获取请假
     * @param array $filter
     * @return array|false|\PDOStatement|string|Model
     */
    public function getLeaveByField($filter=[])
    {
        //默认条件
        $where = [
            'delete_time' => ['eq', 0] //未删除
        ];
        if(!empty($filter) && is_array($filter)){
            $where = array_merge($where,$filter);
        }

        $obj = $this->where($where)->find();

        return $obj;
    }

    /**
     * @param $id
     * @param $cur_time
     * @return bool
     */
    public function deleteLeave($id, $cur_time)
    {
        $result = true;
        if(empty($id)){
            $result = false;
        }else{
            if(empty($cur_time)){
                //当前时间
                $cur_time = time();
            }
            if(is_array($id)){
                //批量删除
                $info = $this->where(['id' => ['in', $id]])->select();
                self::startTrans();
                try{
                    $this->where(['id' => ['in', $id]])->update(['delete_time' => $cur_time]);

                    foreach($info as $value){
                        $data = [
                            'object_id'   => $value['id'],
                            'create_time' => $cur_time,
                            'table_name'  => 'teacher_leave',
                            'name'        => $value['teacher_id']
                        ];
                        //扔进垃圾站
                        Db::name('recycleBin')->insert($data);
                    }

                    self::commit();
                }catch(\Exception $e){
                    self::rollback();
                    $result = false;
                }
            }else{
                //单一删除
                self::startTrans();
                try{
                    $res = $this->where(['id' => $id])->find();
                    $data = [
                        'object_id'   => $res['id'],
                        'create_time' => $cur_time,
                        'table_name'  => 'teacher_leave',
                        'name'        => $res['teacher_id']
                    ];
                    $this->where(['id' => $id])->update(['delete_time' => $cur_time]);
                    Db::name('recycleBin')->insert($data);

                    self::commit();
                }catch(\Exception $e){
                    self::rollback();
                    $result = false;
                }
            }
        }

        return $result;
    }
}